<?php

namespace App\Common;

use App\Constants\DataCode;
use App\Service\common\webSocketServer;
use Hyperf\Config\Annotation\Value;

trait SendMsgUtils
{

    /**
     * 发送登录成功信息
     * @param int $fd
     * @param array $params
     */
    public function sendDeviceLogin(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_LOGIN,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------登录发送数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 发送设备登录心跳信息
     * @param int $fd
     * @param array $params
     */
    public function sendDevicePop(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_POP,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------心跳发送数据');
        // var_dump($data);
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 关闭该设备
     * @param int $fd
     * @return string
     */
    public function closeFdLink(int $fd)
    {
        return make(webSocketServer::class)->close($fd);
    }

    /**
     * 接收到提交信息返回给设备
     * @param int $fd
     * @param array $params
     */
    public function sendAppUploadFriendInfo(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_UPLOAD_FRIEND_INFO,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 发送设备任务
     * @param int $fd
     * @param array $params
     */
    public function sendAppAddFriend(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_ADD_FRIEND,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 发送聊天登录token验证
     * @param int $fd
     * @param array $params
     */
    public function sendAppChatToken(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_CHAT_TOKEN,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 发送聊天信息到服务器
     * @param int $fd
     * @param array $params
     */
    public function sendAppUploadTalkInfo(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_UPLOAD_TALK_INFO,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, json_encode($data, 256));
    }

    /**
     * 发送聊天信息给用户
     * @param int $fd
     * @param array $params
     */
    public function sendAppMessageToFriend(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_MESSAGE_TO_FRIEND,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, str_replace("\/", "/", json_encode($data, 256)));
    }

    /**
     * 发送聊天信息给用户
     * @param int $fd
     * @param array $params
     */
    public function sendAppFriendToMessage(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_FRIEND_TO_MESSAGE,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, str_replace("\/", "/", json_encode($data, 256)));
    }

    /**
     * 同步账号信息保存发送
     * @param int $fd
     * @param array $params
     */
    public function sendAppUploadAccountInfo(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_UPLOAD_ACCOUNT_INFO,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, str_replace("\/", "/", json_encode($data, 256)));
    }

    /**
     * 检查最后一条消息类型
     * @param int $fd
     * @param array $params
     */
    public function sendAppGetFriendLastMessage(int $fd, array $params)
    {
        $data = [
            DataCode::ACCESS_TYPE => DataCode::ACCESS_TYPE_APP_GET_FRIEND_LAST_MESSAGE,
            DataCode::ACCESS_TYPE_DATA => $params,
        ];
        var_dump('---------发送设备任务数据');
        var_dump($data);
        return make(webSocketServer::class)->send($fd, str_replace("\/", "/", json_encode($data, 256)));
    }
}